<!DOCTYPE html>
<html>

	<head>
		<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="../../css/iuapmobile.um.css">
		<link rel="stylesheet" href="../../css/font-icons.css">
		<script src="../../js/summer.js"></script>
		<script src="../../js/jquery.min.js"></script>
		<script src="../../js/Frameworks/iuapmobile.frameworks.ui.js"></script>
	</head>

	<body>
		<div class="um-win" id="index" style="height: 100%;">
			<div class="um-header">
				<a href="#" class="um-back back"> 返回 </a>
				<h3>调整图片方向</h3>
			</div>

			<div class="um-content" style="text-align:center;margin: 0px auto;position:realtive; width:100%; height:100%;vertical-align:middle;display:table-cell;">
				<canvas id="canvas" style="border: 1px solid;display: none;"></canvas>
				<img id="img" style="vertical-align:middle;width: 80%;" src="" />
			</div>

			<div id="divCenter" align="center" style="width: 100%; position: absolute; z-index: 3;  top: 50%;">
				<i class="ti-arrow-left fl pl10" onclick="change(-90)"></i>
				<i class="ti-arrow-right fr pr10" onclick="change(90)"></i>
			</div>

			<div class="um-footer">
				<button class="btns btnOk">
					确定
				</button>
			</div>
		</div>
	</body>

</html>
<script>
	var datasa = {
		id : "", //编号
		base64 : "", //base64
		pageId : "asdasd.dasdad.sadsad" //page ID
	};
	var thisBase64 = "";
	//转换后的数据
	var ol = 0;
	//当前角度，缓存
	var ola = 0;
	//当前度数
	var imageUrl;

	summerready = function() {
		datasa = summer.pageParam;
		imageUrl = summer.pageParam.base64;
		if (summer.getSysInfo().systemType != "android") {
			if(datasa.base64.length < 150){
				getBase64Image(datasa.base64, function(dat) {
					datasa.base64 = dat;
					change(0);
				}, 1);
			}else{
					change(0);
			}
		} else {
			change(0);
		}

		//datasa.base64 = "";
		//thisBase64 = datasa.base64;
		//jz();

		//base = "";
		//thisBase64 = "";
	}
	//转换
	function change(ind) {
		ola = (ol += ind) > 0 ? (ol % 360) : (( n = ol % 360 + 360) == 360 ? 0 : n);
		ola = ola || 360;
		//90 180 270 360
		var canvas = document.getElementById("canvas");
		var img = new Image();
		img.src = datasa.base64;

		img.onload = function() {
			var width;
			var height;
			if (img.width > img.height) {
				height = 1000;
				width = 1000 / img.height * img.width;
			} else {
				width = 1000;
				height = 1000 / img.width * img.height;
			}
			canvas.width = ola / 90 % 2 == 1 ? height : width;
			canvas.height = ola / 90 % 2 == 1 ? width : height;
			var ctx = canvas.getContext("2d");
			ctx.save();

			if (ola / 90 % 2 == 1) {
				ctx.translate(height / 2, width / 2);
				//90度
			} else {
				ctx.translate(width / 2, height / 2);
				//水平
			}

			ctx.rotate(ola * Math.PI / 180);

			if (ola / 90 % 2 == 1) {
				ctx.translate(-(width / 2), -(height / 2));
				//90度
			} else {
				ctx.translate(-(width / 2), -(height / 2));
				//水平
			}

			ctx.drawImage(img, 0, 0, width, height);
			ctx.restore();
			//恢复状态
			var imgData = ctx.getImageData(0, 0, width, height);
			ctx.putImageData(imgData, 0, 0);

			var dataURL = canvas.toDataURL('image/jpeg');
			thisBase64 = dataURL;
			jz();
		};
	}

	//绑定后调整位置
	function jz() {
		var img = document.getElementById("img");
		img.src = thisBase64;
		$("#img").css("margin-top", ($(".um-content").height() - $("#img").height()) / 2);
		setTimeout(function() {
			$("#img").css("margin-top", ($(".um-content").height() - $("#img").height()) / 2);
		}, 100);
	}

	//确定
	$(".btnOk").click(function() {
		datasa.base64 = thisBase64;
		var vimg = JSON.stringify(datasa);
		summer.setAppStorage("pageParamImageBase64", vimg);
		var filepath = imageUrl.substr(0, imageUrl.lastIndexOf("/")) + "/";
		var filename = imageUrl.substr(imageUrl.lastIndexOf("/")).replace("/", "");
		console.log(filepath);
		console.log(filename);
		try {
//			if (check(filepath, filename)) {
//				summer.removeFile({
//					path : filepath,
//					file : filename,
//					callback : "removeCall()"
//				});
//			}else{
				summer.execScript({
					winId : datasa.pageId,
					script : "rotateBack()"
				});
				summer.closeWin();
//			}
		} catch(e) {
			alert("文件移除错误：" + e);
		}
	});
	function removeCall(args) {
		if (args.delStatus == 1) {
			try {
				summer.execScript({
					winId : datasa.pageId,
					script : "rotateBack()"
				});
				summer.closeWin();
			} catch(e) {
			    summer.writeFile("filetest/execScriptErr.txt",e);
			}
		} else {
			alert(args.msg);
		}
	}

	function check(path, name) {
		var flag = summer.exists({
			path : path,
			file : name
		}).toString();
		var txt = flag == "true" ? true : false;
		return txt;
	}
</script>
<script src="../../js/Myjs/Common.js"></script>